/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/3 14:27
 * 用单链表实现一个队列
 */

class Node {
    public int val;
    public Node next;
    public Node(int val){
        this.val = val;
    }
}

public class MyQueue {
    public Node head;
    public Node last;

    /**
     * 尾插法-提供一个offer获取元素
     * @param val
     */
    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            last = last.next;
        }
    }

    /**
     * 弹出列队
     * @return
     */
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int oldVal = head.val;
        this.head = head.next;
        return oldVal;
    }
    public boolean isEmpty(){
        return this.head == null;
    }

    /**
     * 获取队列首元素
     * @return
     */
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        //int oldVal = head.val;
       // this.head = head.next;
        return head.val;
    }
}



